package com.ugoodtech.mdcc.client.controller;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.service.VstarCustomerSatisfactionReportService;
import com.ugoodtech.mdcc.core.Constants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.domain.VstarCustomerSatisfactionReport.VstarCustomerSatisfactionReport;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.service.CustomerSatisfactionReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(description = "客户满意度报告统计")
@RestController
@RequestMapping("/VstarCustomerSatisfactionReport")
public class VstarCustomerSatisfactionReportController {

    @Autowired
    private VstarCustomerSatisfactionReportService vstarCustomerSatisfactionReportService;

    @Autowired
    private CustomerSatisfactionReportService customerSatisfactionReportService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ModuleAuthorityRepository moduleAuthorityRepository;

    @Autowired
    private StationCustomerReportRepository stationCustomerReportRepository;

    @Autowired
    private StationRepository stationRepository;

    @Autowired
    private StationReportRepository stationReportRepository;

    private static final Logger logger = LoggerFactory.getLogger(VstarCustomerSatisfactionReportController.class);


    @ApiOperation("统计满意度查询--改")
    @RequestMapping(value = "customerStat", method = RequestMethod.GET)
    public JsonResponse statReport(Principal principal,
                                   @ApiParam("省份多选") @RequestParam(value = "province", required = false) String province,
                                   @ApiParam("品牌") @RequestParam(value = "brand", required = false) String brand,
                                   @ApiParam("维修站类型") @RequestParam(value = "category", required = false) String category,
                                   @ApiParam("维修站名称") @RequestParam(value = "stationName", required = false) String stationName,
                                   @ApiParam("服务开始时间") @RequestParam(value = "serviceStartTime", required = false) Long serviceStartTime,
                                   @ApiParam("服务结束时间") @RequestParam(value = "serviceEndTime", required = false) Long serviceEndTime,
                                   @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) Long startTime,
                                   @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) Long endTime,
                                   @ApiParam("服务项目") @RequestParam(value = "serviceType", required = false) String serviceType,
                                   @ApiParam("服务次数") @RequestParam(value = "serviceNum", required = false) Integer serviceNum,
                                   @ApiParam("技术人员姓名") @RequestParam(value = "userName", required = false) String userName,
                                   @ApiParam("部门") @RequestParam(value = "department", required = false) String department,
                                   @ApiParam("城市") @RequestParam(value = "city", required = false) String city,
                                   @ApiParam("客户状态ID") @RequestParam(value = "customerStat", required = false) String customerStat,
                                   @ApiParam("客户类型ID") @RequestParam(value = "customerType", required = false) String customerType,
                                   @ApiParam("油漆品牌ID") @RequestParam(value = "paintBrand", required = false) String paintBrand,
                                   @ApiParam("品牌定位ID") @RequestParam(value = "productPosition", required = false) String productPosition,
                                   @ApiParam("常规服务报告状态") @RequestParam(value = "reportStatus", required = false) String reportStatus,
                                   @ApiParam("满意度打分") @RequestParam(value = "score", required = false) String score,
                                   @ApiParam("是否进行满意度回访") @RequestParam(value = "isManaged", required = false) String isManaged,
                                   @ApiParam("需跟进报告处理状态") @RequestParam(value = "serviceReportStatus", required = false) String serviceReportStatus,
                                   @ApiParam("是否填写精益钣喷：true:填写，false:未填写") @RequestParam(value = "paintType", required = false) Boolean paintType,
                                   @ApiParam("该维修站是否使用我司增值服务") @RequestParam(value = "payService", required = false) String payService,
                                   @ApiParam("油漆配送时效") @RequestParam(value = "sentTime", required = false) String sentTime,
                                   @ApiParam("经销商上门服务周期") @RequestParam(value = "serviceTime", required = false) String serviceTime,
                                   @ApiParam(value = "维修站编号") @RequestParam(value = "stationNum", required = false) String stationNum
    ) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("500", "未找到此用户");
        }
//        ModuleAuthority m = moduleAuthorityRepository.findByName(Constants.REPORT_STATISTICS_WECHAT);
//        boolean b = clientUser.getAuthorities().contains(m);
//        if (b == false) {
//            return JsonResponse.errorResponseWithError("抱歉", "您没有此权限:您没有此权限:报告统计（微信端）");
//        }
        com.ugoodtech.mdcc.core.dto.JsonResponse response = customerSatisfactionReportService.statReport(province, brand, category, stationName, serviceStartTime,
                serviceEndTime, startTime, endTime, serviceType, serviceNum, clientUser.getId(), userName, department, city, customerStat, customerType, paintBrand,
                productPosition, reportStatus, score, isManaged, serviceReportStatus, paintType, payService, sentTime, serviceTime, stationNum);
        if (response == null) {
            return JsonResponse.errorResponseWithError("抱歉", "请重新搜索");
        }
        return JsonResponse.successResponseWithData(response);
    }

    @ApiOperation("获取满意度分数表格--改")
    @RequestMapping(value = "getScore", method = RequestMethod.GET)
    public JsonResponse getScore(Principal principal,
                                 @ApiParam("省份多选") @RequestParam(value = "province", required = false) String province,
                                 @ApiParam("品牌") @RequestParam(value = "brand", required = false) String brand,
                                 @ApiParam("维修站类型") @RequestParam(value = "category", required = false) String category,
                                 @ApiParam("维修站名称") @RequestParam(value = "stationName", required = false) String stationName,
                                 @ApiParam("服务开始时间") @RequestParam(value = "serviceStartTime", required = false) Long serviceStartTime,
                                 @ApiParam("服务结束时间") @RequestParam(value = "serviceEndTime", required = false) Long serviceEndTime,
                                 @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) Long startTime,
                                 @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) Long endTime,
                                 @ApiParam("服务项目") @RequestParam(value = "serviceType", required = false) String serviceType,
                                 @ApiParam("服务次数") @RequestParam(value = "serviceNum", required = false) Integer serviceNum,
                                 @ApiParam("技术人员姓名") @RequestParam(value = "userName", required = false) String userName,
                                 @ApiParam("部门") @RequestParam(value = "department", required = false) String department,
                                 @ApiParam("城市") @RequestParam(value = "city", required = false) String city,
                                 @ApiParam("客户状态ID") @RequestParam(value = "customerStat", required = false) String customerStat,
                                 @ApiParam("客户类型ID") @RequestParam(value = "customerType", required = false) String customerType,
                                 @ApiParam("油漆品牌ID") @RequestParam(value = "paintBrand", required = false) String paintBrand,
                                 @ApiParam("品牌定位ID") @RequestParam(value = "productPosition", required = false) String productPosition,
                                 @ApiParam("常规服务报告状态") @RequestParam(value = "reportStatus", required = false) String reportStatus,
                                 @ApiParam("满意度打分") @RequestParam(value = "score", required = false) String score,
                                 @ApiParam("是否进行满意度回访") @RequestParam(value = "isManaged", required = false) String isManaged,
                                 @ApiParam("需跟进报告处理状态") @RequestParam(value = "serviceReportStatus", required = false) String serviceReportStatus,
                                 @ApiParam("是否填写精益钣喷：true:填写，false:未填写") @RequestParam(value = "paintType", required = false) Boolean paintType,
                                 @ApiParam("该维修站是否使用我司增值服务") @RequestParam(value = "payService", required = false) String payService,
                                 @ApiParam("油漆配送时效") @RequestParam(value = "sentTime", required = false) String sentTime,
                                 @ApiParam("经销商上门服务周期") @RequestParam(value = "serviceTime", required = false) String serviceTime,
                                 @ApiParam(value = "维修站编号") @RequestParam(value = "stationNum", required = false) String stationNum) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("500", "未找到此用户");
        }
        JsonResponse response = vstarCustomerSatisfactionReportService.getScore(province, brand, category, stationName, serviceStartTime,
                serviceEndTime, startTime, endTime, serviceType, serviceNum, clientUser.getId(), userName, department, city, customerStat, customerType, paintBrand,
                productPosition, reportStatus, score, isManaged, serviceReportStatus, paintType, payService, sentTime, serviceTime, stationNum);
        return JsonResponse.successResponseWithData(response);
    }

//    @ApiOperation("获取满意度分数表格--改")
//    @RequestMapping(value = "getScore", method = RequestMethod.GET)
//    public JsonResponse getScore(Principal principal,
//                                 @ApiParam("省份多选") @RequestParam(value = "provinces", required = false) String provinces,
//                                 @ApiParam("品牌") @RequestParam(value = "brandname", required = false) String brandname,
//                                 @ApiParam("维修站类型") @RequestParam(value = "category", required = false) String category,
//                                 @ApiParam("维修站名称") @RequestParam(value = "name", required = false) String name,
//                                 @ApiParam("服务开始时间") @RequestParam(value = "serviceStartTime", required = false) Long serviceStartTime,
//                                 @ApiParam("服务结束时间") @RequestParam(value = "serviceEndTime", required = false) Long serviceEndTime,
//                                 @ApiParam("服务项目") @RequestParam(value = "serviceType", required = false) Long serviceType,
//                                 @ApiParam("服务次数") @RequestParam(value = "serviceNum", required = false) Integer serviceNum,
//                                 @ApiParam("技术人员姓名") @RequestParam(value = "userName", required = false) String userName) {
//        User clientUser = userRepository.findByUsername(principal.getName());
//        if (clientUser == null) {
//            return JsonResponse.errorResponseWithError("500", "未找到此用户");
//        }
//        ModuleAuthority m = moduleAuthorityRepository.findByName(Constants.REPORT_STATISTICS_WECHAT);
//        boolean b = clientUser.getAuthorities().contains(m);
//        if (b == false) {
//            return JsonResponse.errorResponseWithError("抱歉", "您没有此权限:您没有此权限:报告统计（微信端）");
//        }
//        QStationReport qStationReport = QStationReport.stationReport;
//        BooleanBuilder builder = new BooleanBuilder();
//        builder.and(qStationReport.deleted.eq(false));
//        builder.and(qStationReport.enabled.eq(true));
//        builder.and(qStationReport.category.eq(Constants.CUSTOMER_REPORT));
//        if (StringUtils.isNotEmpty(provinces)) {
//            builder.and(qStationReport.station.province.eq(provinces));
//        }
//        if (StringUtils.isNotEmpty(brandname)) {
//            builder.and(qStationReport.station.brand.eq(brandname));
//        }
//        if (StringUtils.isNotEmpty(category)) {
//            builder.and(qStationReport.station.category.eq(category));
//        }
//        if (StringUtils.isNotEmpty(name)) {
//            builder.and(qStationReport.station.name.like("%" + name + "%"));
//        }
//        List<StationReport> stationList = (List<StationReport>) stationReportRepository.findAll(builder);
//        logger.info("H5--满意度报告有：" + stationList.size() + "份");
//        StringBuffer stringBuffer = new StringBuffer("");
//        for (StationReport station : stationList) {
//            stringBuffer.append("'" + station.getStation().getId() + "',");
//        }
//        stringBuffer = new StringBuffer(stringBuffer.toString().substring(1, stringBuffer.length()));
//        stringBuffer = new StringBuffer(stringBuffer.toString().substring(0, stringBuffer.length() - 2));
//        logger.info("H5--处理之后的stationIds：" + stringBuffer.toString());
////        stringBuffer.toString().substring(0, stringBuffer.length() - 1);
//        String startTime = null;
//        String endTime = null;
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.DEFAULT_DATE_FORMAT);
//        if (serviceStartTime != null) {
//            startTime = simpleDateFormat.format(new Date(serviceStartTime));
//            endTime = simpleDateFormat.format(new Date(serviceEndTime));
//        }
//        return JsonResponse.successResponseWithData(stationCustomerReportRepository.getSatisfactionReport(startTime, endTime,
//                stringBuffer.toString(), null,
//                userName == null ? null : userName, serviceNum == null ? null : serviceNum.toString(), serviceType == null ? null : serviceType.toString()));
//    }


}
